package org.cloud.im.admin.processor;

import org.apache.commons.lang3.StringUtils;
import org.cloud.im.admin.handler.StoreHandler;
import org.cloud.im.admin.listener.ImAdminGroupListener;
import org.cloud.im.admin.listener.ImAdminUserListener;
import org.cloud.im.core.packets.Command;
import org.cloud.im.core.utils.PropUtil;
import org.cloud.im.server.CloudImServer;
import org.cloud.im.server.command.CommandManager;
import org.cloud.im.server.command.handler.ChatReqHandler;
import org.cloud.im.server.command.handler.HandshakeReqHandler;
import org.cloud.im.server.command.handler.LoginReqHandler;
import org.cloud.im.server.config.ImServerConfig;
import org.cloud.im.server.config.PropertyImServerConfigBuilder;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.tio.core.ssl.SslConfig;

/**
 * @Author: w.weidong.lin@abite.com
 * @Description: IM服务端DEMO演示启动类
 * @Date: create in 2021/10/23 9:17
 */
@Component
@Order(value = 1)
public class ImAdminServerProcessor implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // TODO 测试阶段用于初始化数据
        StoreHandler.initStoreData();
        ImServerConfig imServerConfig = new PropertyImServerConfigBuilder("config/im.properties").build();
        //初始化SSL;(开启SSL之前,你要保证你有SSL证书哦...)
        initSsl(imServerConfig);
        //设置群组监听器，非必须，根据需要自己选择性实现;
        imServerConfig.setImGroupListener(new ImAdminGroupListener());
        //设置绑定用户监听器，非必须，根据需要自己选择性实现;
        imServerConfig.setImUserListener(new ImAdminUserListener());
        CloudImServer imServer = new CloudImServer(imServerConfig);

        /*****************start 以下处理器根据业务需要自行添加与扩展，每个Command都可以添加扩展,此处为demo中处理**********************************/

        HandshakeReqHandler handshakeReqHandler = CommandManager.getCommand(Command.COMMAND_HANDSHAKE_REQ, HandshakeReqHandler.class);
        //添加自定义握手处理器;
        handshakeReqHandler.addMultiProtocolProcessor(new AdminWsHandshakeProcessor());
        LoginReqHandler loginReqHandler = CommandManager.getCommand(Command.COMMAND_LOGIN_REQ, LoginReqHandler.class);
        //添加登录业务处理器;
        loginReqHandler.setSingleProcessor(new LoginServiceProcessor());
        //添加用户业务聊天记录处理器，用户自己继承抽象类BaseAsyncChatMessageProcessor即可，以下为内置默认的处理器！
        ChatReqHandler chatReqHandler = CommandManager.getCommand(Command.COMMAND_CHAT_REQ, ChatReqHandler.class);
        chatReqHandler.setSingleProcessor(new AdminWsChatMessageProcessor());
        /*****************end *******************************************************************************************/
        imServer.start();
    }

    /**
     * 开启SSL之前，你要保证你有SSL证书哦！
     *
     * @param imServerConfig
     * @throws Exception
     */
    private void initSsl(ImServerConfig imServerConfig) throws Exception {
        //开启SSL
        if (ImServerConfig.ON.equals(imServerConfig.getIsSSL())) {
            String keyStorePath = PropUtil.get("cloudIm.key.store.path");
            String keyStoreFile = keyStorePath;
            String trustStoreFile = keyStorePath;
            String keyStorePwd = PropUtil.get("cloudIm.key.store.pwd");
            if (StringUtils.isNotBlank(keyStoreFile) && StringUtils.isNotBlank(trustStoreFile)) {
                SslConfig sslConfig = SslConfig.forServer(keyStoreFile, trustStoreFile, keyStorePwd);
                imServerConfig.setSslConfig(sslConfig);
            }
        }
    }
}
